//
//  NSString+Timestamp.m
//  jsy
//
//  Created by  任辰东 on 2020/4/13.
//  Copyright © 2020 JSY. All rights reserved.
//

#import "NSString+Timestamp.h"

@implementation NSString (Timestamp)

- (NSString *)dateFormatterTimeForHFW
{
    NSString *dateString;
    
    NSTimeInterval timeInterval = (NSTimeInterval)(self.longLongValue);
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    
    NSDate *today = [NSDate date];
    
    NSDateFormatter* yaerDateFormatter = [NSDateFormatter new];
    yaerDateFormatter.dateFormat = @"yyyy";
    
    NSString *thisYearString = [yaerDateFormatter stringFromDate:today];//今年
    NSString *yearString = [yaerDateFormatter stringFromDate:date];//时间戳 所在年份
    
    NSDateFormatter* dateFormatter = [NSDateFormatter new];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString *todayString = [dateFormatter stringFromDate:today];
    NSDate *today_start = [dateFormatter dateFromString:todayString];
    
    [dateFormatter setDateFormat:nil];
    
    if ( date == [date laterDate:today_start])
    {
        //显示时分
        dateFormatter.dateFormat = @"HH:mm";
        dateString = [dateFormatter stringFromDate:date];

    }
    else if ([thisYearString isEqualToString:yearString])
    {
        //显示月日  同一年
        dateFormatter.dateFormat = @"MM-dd";
        dateString = [dateFormatter stringFromDate:date];

    }
    else
    {
        //显示年月日
        dateFormatter.dateFormat = @"yyyy-MM-dd";
        dateString = [dateFormatter stringFromDate:date];
    }
    return dateString;
}

//根据时间戳 今天 昨天 前天  具体时间
- (NSString *)dateFormatterTime
{
    NSString *dateString;
    NSTimeInterval timeInterval = (NSTimeInterval)(self.longLongValue);
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    
    NSDate *today = [NSDate date];
    NSDateFormatter* dateFormatter = [NSDateFormatter new];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString *todayString = [dateFormatter stringFromDate:today];
    NSDate *today_start = [dateFormatter dateFromString:todayString];
    NSDate *oneSecond = [today_start dateByAddingTimeInterval:- 60];
    NSDate *oneMinute = [today_start dateByAddingTimeInterval:- (59 * 60)];
    NSDate *oneHours = [today_start dateByAddingTimeInterval:- (60 * 60)];
    NSDate *beforeToday = [today_start dateByAddingTimeInterval:-(23 * 60 * 60)];
    NSDate *yesterday_start = [today_start dateByAddingTimeInterval:-(24 * 60 * 60)];
    NSDate *yesterdayBefore_start = [today_start dateByAddingTimeInterval:-(24 * 60 * 60 * 2)];
    
    [dateFormatter setDateFormat:nil];
    if (date == [date laterDate:oneSecond])
    {
        dateString = @"一分钟前";
    }
    else if (date == [date laterDate:oneMinute])
    {
        dateString = @"59分钟前";
    }
    else if (date == [date laterDate:oneHours])
    {
        dateString = @"一小时前";
    }
    else if (date == [date laterDate:beforeToday])
    {
        dateString = @"23小时前";
    }
    else if (date == [date laterDate:yesterday_start])
    {
        dateString = @"昨天";
    }
    else if (date == [date laterDate:yesterdayBefore_start])
    {
        dateString = @"前天";
    }
    else
    {
        //显示时期
        dateFormatter.dateFormat = @"yyyy-MM-dd";
        dateString = [dateFormatter stringFromDate:date];
    }
    return dateString;
}

//根据时间戳 返回周几
- (NSString *)dateFormatterSimple
{
    NSString *dateString;
    
    NSTimeInterval timeInterval = (NSTimeInterval)(self.longLongValue);
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    
    NSDate *today = [NSDate date];
    NSDateFormatter* dateFormatter = [NSDateFormatter new];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString *todayString = [dateFormatter stringFromDate:today];
    NSDate *today_start = [dateFormatter dateFromString:todayString];
    NSDate *yesterday_start = [today_start dateByAddingTimeInterval:-(24 * 60 * 60)];
    
    [dateFormatter setDateFormat:nil];
    if (date == [date laterDate:today_start])
    {
        //显示时分
        dateFormatter.timeStyle = NSDateFormatterShortStyle;
        dateString = [dateFormatter stringFromDate:date];
        
        dateString = @"今天";
    }
    else if (date == [date laterDate:yesterday_start])
    {
        //显示昨天
        dateString = @"昨天";
    }
    else
    {
        //显示周几
        dateFormatter.dateFormat = @"EEEE";
        dateString = [dateFormatter stringFromDate:date];
    }
    return dateString;
}

- (NSString *)dateFormatterDetail
{
    NSString *dateString;
    
    NSTimeInterval timeInterval = (NSTimeInterval)(self.longLongValue);
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDate *today = [NSDate date];
    NSDateFormatter* dateFormatter = [NSDateFormatter new];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString *todayString = [dateFormatter stringFromDate:today];
    NSDate *today_start = [dateFormatter dateFromString:todayString];
    NSDate *yesterday_start = [today_start dateByAddingTimeInterval:-(24 * 60 * 60)];
    
    [dateFormatter setDateFormat:nil];
    if (date == [date laterDate:today_start])
    {
    
        dateString = @"今天";
    }
    else if (date == [date laterDate:yesterday_start])
    {
        //显示昨天
        dateString = @"昨天";
    }
    else
    {
        //显示周几
        dateFormatter.dateFormat = @"EEEE";
        dateString = [dateFormatter stringFromDate:date];
    }
    
    //显示时分
    dateFormatter.timeStyle = NSDateFormatterShortStyle;
    dateString = [NSString stringWithFormat:@"%@ %@",dateString, [dateFormatter stringFromDate:date]];
    
    return dateString;
}

- (NSString *)shortDateFormatter
{
    NSTimeInterval timeInterval = (NSTimeInterval)(self.longLongValue);
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    
    NSDateFormatter* dateFormatter = [NSDateFormatter new];
    dateFormatter.dateFormat = @"MM月dd日";
    NSString *formatterString = [dateFormatter stringFromDate:date];
    return formatterString;
}

- (NSString *)LongDateFormatter
{
    NSTimeInterval timeInterval = (NSTimeInterval)(self.longLongValue);
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    
    NSDateFormatter* dateFormatter = [NSDateFormatter new];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString *formatterString = [dateFormatter stringFromDate:date];
    return formatterString;
}

- (NSString *)dateStringWithFormat:(NSString *)format
{
    NSTimeInterval timeInterval = (NSTimeInterval)(self.longLongValue);
    if (self.doubleValue > 100000000000) {
        //毫秒表示
        timeInterval = (NSTimeInterval)(self.doubleValue)/1000;
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    
    NSDateFormatter* dateFormatter = [NSDateFormatter new];
    dateFormatter.dateFormat = format;
    return [dateFormatter stringFromDate:date];
}

-(NSTimeInterval)getTimestampWithFormat:(NSString *)format
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    NSDate *date = [formatter dateFromString:self];
    return [date timeIntervalSince1970];
}

+ (NSString *)getNowTimeTimestamp
{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    
    NSTimeInterval a=[dat timeIntervalSince1970];
    
    NSString*timeString = [NSString stringWithFormat:@"%0.f", a];//转为字符型
    
    return timeString;
}

+ (NSString*)getCurrentTimesWithFormat:(NSString *)format
{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:format];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    return currentTimeString;
    
}

@end
